﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP.App_UI.UserControls
{
    public partial class DatePicker : System.Web.UI.UserControl
    {
        private void Page_Load(object sender, EventArgs e)
        {

        }

        public void SetValue(object date)
        {
            DateTime dtm;
            if (date == null || !DateTime.TryParse(date.ToString(), out dtm))
            {
                txtDate.Value = "";
            }
            else
            {
                switch (date.GetType().ToString())
                {
                    case "System.DateTime":
                        txtDate.Value = ((DateTime)date).ToShortDateString();
                        break;
                    case "System.String":
                    default:
                        txtDate.Value = date.ToString();
                        break;
                }
            }
        }

        public string GetStringValue()
        {
            string strDate = txtDate.Value;

            return strDate;
        }

        public Nullable<DateTime> GetDateValue()
        {
            string strDate = txtDate.Value;
            Nullable<DateTime> date = ConvertNullDateTime(strDate);

            return date;
        }

        public DateTime Value
        {
            get
            {
                DateTime? date = GetDateValue();
                return date.HasValue ? date.Value : DateTime.MinValue;
            }
            set
            {
                SetValue(value);
            }
        }

        public string ValueS
        {
            get
            {
                DateTime? date = GetDateValue();
                return (date.HasValue ? date.Value : DateTime.MinValue).ToShortDateString();
            }
            set
            {
                SetValue(value);
            }
        }

        protected Nullable<DateTime> ConvertNullDateTime(Object o)
        {
            Nullable<DateTime> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<DateTime>)Convert.ToDateTime(o)));
            return result;
        }
    }
}